package com.foreknow.oop.hw8;

import java.util.Scanner;

public class Game {
    private static int BATTLE_NUMBER = 0;   // 对战次数
    public static void main(String[] args) {
        Game game = new Game();
        User user = new User();
        Computer computer = new Computer();
        game.initial();
        game.startGame(user,computer);
    }

    /**
     * 初始化
     */
    public void initial(){
        System.out.println("---------------------欢 迎 进 入 游 戏 世 界---------------------\n");
        System.out.println("                     *********************");
        System.out.println("                     *****  猜拳,开始 *****");
        System.out.println("                     *********************\n");
    }

    /**
     * 开始游戏
     * @param user 用户
     * @param computer 电脑
     */
    public void startGame(User user, Computer computer){
        Scanner sc = new Scanner(System.in);
        System.out.println("出拳规则：1、剪刀 2、石头 3、布");
        System.out.print("请选择对方角色（1、刘备 2、孙权 3、曹操）：");
        int computerUserId = sc.nextInt();
        computer.setComputerUser(Util.getComputerUser(computerUserId - 1));
        System.out.print("请输入你的姓名：");
        String userName = sc.next();
        user.setUser(userName);
        System.out.printf("%s VS %s对战\n\n",userName,computer.getComputerUser());
        System.out.print("要开始吗？（y/n）");
        String choose = sc.next();
        System.out.println();
        while (choose.equals("y") || choose.equals("Y")){
            BATTLE_NUMBER++;
            int userPunch = user.showFirst();
            int computerPunch = computer.showFirst();
            int compareResult = compare(userPunch, computerPunch);
            int userScore = user.getScore();
            userScore += compareResult > 0 ? 1 : 0;
            user.setScore(userScore);
            int computerScore = computer.getScore();
            computerScore += compareResult < 0 ? 1 : 0;
            computer.setScore(computerScore);
            System.out.printf("结果：%s\n\n",getResult(compareResult));
            System.out.print("是否开始下一轮（y/n）:");
            choose = sc.next();
        }
        showBattleResult(user,computer);
    }

    /**
     * 显示对战结果
     * @param user 用户
     * @param computer 角色
     */
    public void showBattleResult(User user,Computer computer){
        System.out.println("---------------------------------------------------------------------\n");
        System.out.printf("%s VS %s\n",computer.getComputerUser(),user.getUser());
        System.out.printf("对战次数：%d\n\n",BATTLE_NUMBER);

        System.out.println("姓名\t得分");
        System.out.printf("%s\t%d\n",user.getUser(),user.getScore());
        System.out.printf("%s\t%d\n\n",computer.getComputerUser(),computer.getScore());

        if (user.getScore() == computer.getScore()){
            System.out.println("结果：打成平手，下次再和你一分高下！");
        }else if (user.getScore() <= computer.getScore()){
            System.out.println("结果：呵呵，笨笨，下次加油啊！");
        }else{
            System.out.println("结果：可恶，这次算你运气好，下次我一定会赢的！");
        }
        System.out.println("---------------------------------------------------------------------\n");
    }

    /**
     * 比较输赢
     * @param userPunch 用户出拳
     * @param computerPunch 电脑出拳
     * @return
     */
    public int compare(int userPunch,int computerPunch){
        if (userPunch == 1 && computerPunch == 2
                || userPunch == 2 && computerPunch == 3
                || userPunch == 3 && computerPunch == 1){
            return -1;
        }else if (userPunch == computerPunch){
            return 0;
        }else {
            return 1;
        }
    }

    /**
     * 获得对战结果
     * @param result 结果Id
     * @return
     */
    public String getResult(int result){
        switch (result){
            case -1:
                return "^_^，你输了，真笨！";
            case 0:
                return "和局，真衰！嘿嘿，等着瞧吧！";
            default:
                return "╰_╯，你赢了，算你运气好！";
        }
    }
}
